<template>
	<view style="width: 100%;height: 100%;">
		<view v-if="activityList.length>0" class="cu-custom" :style="[{height:navBgImageHeight + 'px'}]">
			<swiper @change="changeActivity" class="header-bg" :circular="true" :autoplay="true"
				indicator-color="#FFFFFF" indicator-active-color="#8799a3" :indicator-dots="true">
				<swiper-item v-for="(item,index) in activityList" :key="index" item-id="index">
					<image @tap="jumpToActivity(item)" :src="item.image+'?imageView2/2/w/800/h/480/q/75'"
						class="banner-image"></image>
				</swiper-item>
			</swiper>
			<view class="cu-bar fixed"
				:style="'height:'+navHeight+'px;padding-top:'+statusHeight+'px;opacity:'+searchOpacity+';'">
				<view @tap="jumpToSearchAction" class="flex justify-center align-center"
					style="z-index: 99;position: absolute;left: 50upx;width: 44upx;height: 44upx;">
					<image src="../../static/images/setting/search.png" style="width: 100%;height: 100%;"></image>
				</view>
			</view>
			<view :class="'cu-bar fixed '+themeColor"
				:style="'width:100%;height:'+navHeight+'px;padding-top:'+statusHeight+'px;opacity:'+navOpacity+';'">
				<view @tap="jumpToSearchAction" class="flex justify-center align-center"
					style="width: 44upx;height: 44upx;">
					<image src="../../static/images/setting/search.png" style="left: 50upx;width: 100%;height: 100%;">
					</image>
				</view>
				<!-- #ifdef MP-WEIXIN -->
				<view class="flex justify-center align-center" style="width: 60%;">
					<!-- #endif -->
					<!-- #ifndef MP-WEIXIN -->
					<view class="flex justify-center align-center" style="width: 100%;">
						<!-- #endif -->
						<text class="text-bold text-xl text-white">{{appName}}</text>
					</view>
				</view>
			</view>
			<cu-custom v-else :isBack="false" :showLeftIcon="true" leftIcon="search" @leftBtnClick="jumpToSearchAction">
				<block slot="content">{{appName}}</block>
			</cu-custom>
			<view v-if="totalInfo" class="solid-bottom flex justify-between align-center bg-white padding-sm text-sm text-grey">
				<text>总内容:{{format(totalInfo.post_count)}}</text>
				<text>Q友:{{format(totalInfo.member_count)}}</text>
				<text>圈子:{{format(totalInfo.circle_count)}}</text>
				<text>今日发表:{{format(totalInfo.today)}}</text>
				<text>今日签到:{{format(totalInfo.sign_count)}}</text>
			</view>
			<view class="flex justify-around align-center padding bg-white">
				<view v-if="hasShop==1" @tap="jumpToMall" class="flex justify-start align-center" style="flex-direction: column;">
					<view class="radius">
						<image src="../../static/images/setting/shop.png" class="middle-icon"></image>
					</view>
					<text class="text-df">橱窗</text>
				</view>
				<view @tap="jumpToSignSort" class="flex justify-start align-center" style="flex-direction: column;">
					<view class="radius">
						<image src="../../static/images/setting/index-sign.png" class="middle-icon"></image>
					</view>
					<text class="text-df text-black">签到</text>
				</view>
				<view @tap="jumpTopicList" class="flex justify-start align-center" style="flex-direction: column;">
					<view class="radius">
						<image src="../../static/images/setting/index-news.png" class="middle-icon"></image>
					</view>
					<text class="text-df text-black">话题热点</text>
				</view>
				<view @tap="jumpToAllSort" class="flex justify-start align-center" style="flex-direction: column;">
					<view class="radius">
						<image src="../../static/images/setting/index-sort.png" class="middle-icon"></image>
					</view>
					<text class="text-df text-black">排行榜</text>
				</view>
			</view>
			<view v-if="topNews.length>0"
				class="flex justify-start align-center bg-white solid-top padding-top-xs padding-bottom-xs padding-right-xs padding-left-sm">
				<image src="../../static/images/setting/top-news.png" style="width: 60px;height: 20px;"></image>
				<text class="text-gray padding-left-xs">|</text>
				<swiper style="width: 100%;height: 20px;padding-left: 5px;" :circular="true" :autoplay="true"
					:vertical="true" :indicator-dots="false">
					<swiper-item @tap="jumpDetail(item)" class="flex justify-start align-center" v-for="(item,index) in topNews" :key="index"
						item-id="index">
						<text>{{item.title}}</text>
					</swiper-item>
				</swiper>
			</view>
			<view class="bg-gray" style="height: 5px;"></view>
			<view v-if="forumList.length>1">
				<uni-swiper-dot :dotsStyles="dotStyle" :info="forumList" :current="forumSwiperCurrent" field="content"
					mode="round" style="width: 60upx;height: 20upx;">
					<swiper @change="changeForumSwiper" :circular="true" :indicator-dots="false"
						style="width: 100%;height: 190upx;background-color: #FFF;">
						<swiper-item v-for="(item,index) in forumList" :key="index" style="width: 100%;">
							<view class="flex justify-center align-center bg-white"
								style="flex-direction: column;width: 100%;">
								<view v-for="(subItem,subIndex) in item" :key="subIndex" style="width: 100%;">
									<view class="flex" style="width: 100%;">
										<view class="flex" style="width: 100%;"
											v-for="(innerItem,innerIndex) in subItem" :key="innerIndex">
											<view @tap="jumpToForumPostListByItem(innerItem)"
												class="flex-sub flex justify-center align-center padding-sm solid-right solid-bottom">
												<image :src="innerItem.icon" style="width: 15px;height: 15px;"></image>
												<text class="padding-left-xs">{{innerItem.name}}</text>
											</view>
										</view>
									</view>
								</view>
							</view>
						</swiper-item>
					</swiper>
				</uni-swiper-dot>
			</view>
			<view v-else-if="forumList.length>0" v-for="(item,index) in forumList" :key="index" style="width: 100%;">
				<view class="flex justify-center align-center bg-white" style="flex-direction: column;width: 100%;">
					<view v-for="(subItem,subIndex) in item" :key="subIndex" style="width: 100%;">
						<view class="flex" style="width: 100%;">
							<view class="flex" style="width: 100%;" v-for="(innerItem,innerIndex) in subItem"
								:key="innerIndex">
								<view @tap="jumpToForumPostListByItem(innerItem)"
									class="flex-sub flex justify-center align-center padding-sm solid-right solid-bottom">
									<image :src="innerItem.icon" style="width: 15px;height: 15px;"></image>
									<text class="padding-left-xs">{{innerItem.name}}</text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="bg-gray" style="height: 5px;"></view>
			<view v-if="activityList.length>0"
				:style="[scrollTop>=floatMenuFixedTop?'z-index:110;width: 100%;height:50rpx;position:fixed;':'width: 100%;height: 85rpx;']+[scrollTop>=floatMenuFixedTop?'top:'+fixNavTop+'px;':'']"
				class="flex justify-start align-start float-menu">
				<v-tabs v-model="currentTab" :ratio="ratio" :fixed="false" height="50rpx" :padding="tabPadding+'px 0px'"
					pills-border-radius="60rpx" bg-color="#fff" :tabs="tabs" :pills="true" :bold="true"
					activeFontSize="32rpx" fontSize="32rpx" activeColor="#FFF" pillsColor="#1cbbb4"
					:scroll="enableNavForum" @change="changeTab" lineColor="#fff"
					:style="enableNavForum?'width: 90%;':'width: 100%;'"></v-tabs>
				<view v-if="enableNavForum" @tap="jumpToForumList"
					class="bg-white flex justify-center align-center solid-bottom solid-left text-xl"
					style="width: 80rpx;height: 85rpx;flex-direction: column;">
					<text class="cuIcon-list text-gray"></text>
				</view>
			</view>
			<view v-else style="width: 100%;" class="flex justify-start align-start">
				<v-tabs v-model="currentTab" :ratio="ratio" :fixed="true" height="50rpx" :padding="tabPadding+'px 0px'"
					pills-border-radius="60rpx" bg-color="#fff" :tabs="tabs" :pills="true" :bold="true"
					activeFontSize="32rpx" fontSize="32rpx" activeColor="#FFF" pillsColor="#1cbbb4"
					:scroll="enableNavForum" @change="changeTab" lineColor="#fff"
					:style="enableNavForum?'width: 90%;':'width: 100%;'"></v-tabs>
				<view v-if="enableNavForum" @tap="jumpToForumList"
					class="bg-white flex justify-center align-center solid-bottom solid-left text-xl"
					style="width: 80rpx;height: 85rpx;flex-direction: column;">
					<text class="cuIcon-list text-gray"></text>
				</view>
			</view>
			<view v-if="currentTab==0 && hasAttention == false">
				<view v-if="isUserLogined==false">
					<view style="height: 50vh;">
						<view class="flex justify-center align-center"
							:style="'flex-direction: column;height: '+[activityList.length>0?50:80]+'vh;'">
							<view class="flex justify-start align-center" style="flex-direction: column;">
								<image src="../../static/images/setting/index_attention.png"
									style="width: 66upx;height: 66upx;"></image>
								<view style="height: 2upx;"></view>
								<text class="text-grey">登录同步关注状态</text>
							</view>
							<view style="height: 20upx;"></view>
							<view class="flex justify-center align-center" style="width: 100%;">
								<button @tap="loginNowAction" class="cu-btn bg-green radius" style="width: 40%;">立即登录</button>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view v-if="currentTab==3">
				<view class="solid-bottom" style="width: 100%;">
					<scroll-view scroll-x class="bg-white nav" scroll-with-animation
						:scroll-left="subscribeTab.scrollLeft">
						<view class="cu-item" :class="index==subscribeTab.current?'text-blue cur':''"
							v-for="(item,index) in subscribeTab.items" :key="index" @tap="tabSelect" :data-id="index">
							{{item.name}}
						</view>
					</scroll-view>
				</view>
			</view>
			<!-- 			<view v-if="currentTab==2">
				<view class="solid-bottom" style="width: 100%;">
					<scroll-view scroll-x class="bg-white nav" scroll-with-animation :scroll-left="circleTab.scrollLeft">
						<view class="cu-item" :class="index==circleTab.current?'text-blue cur':''" v-for="(item,index) in circleTab.items"
							:key="index" @tap="tabSelect" :data-id="index">
							{{item.name}}
						</view>
					</scroll-view>
				</view>
			</view> -->
			<view v-if="currentTab==0&&hasAttention==false&&isUserLogined==true" class="padding-top-sm padding-bottom">
				<text class="text-bold text-lg padding-left">这些人很有趣哦~</text>
				<view v-for="(subList,index) in recommendUserList" :key="index"
					class="flex justify-start align-center" style="flex-direction: column;width: 100%;">
					<view class="flex justify-around align-center padding-top" style="width: 100%;">
						<view v-for="(item,subIndex) in subList" :key="subIndex" style="width: 25%;">
							<recommend-user-item @attention="attentionUserAction(item,index,subIndex)" :userInfo="item"></recommend-user-item>
						</view>
					</view>
				</view>
				<view style="height: 10px;"></view>
				<text class="text-bold text-lg padding-left padding-top-sm">这些话题大家都在关注哦~</text>
				<view v-for="(topic,index) in recommendTopicList" :key="index">
					<topic-item :topic="topic" @tap="clickTopicItem(topic)" @attention="attentionTopicAction(topic,index)"></topic-item>
				</view>
				<view style="height: 10px;"></view>
				<view class="flex justify-around align-center" style="width: 100%;">
					<button @tap="confirmAttentionAction" class="cu-btn round bg-gradual-blue" style="width: 35%;">我选好了</button>
					<button @tap="initAttentionRecommendList" class="cu-btn round bg-gradual-orange" style="width: 35%;">换一批~</button>
				</view>
			</view>
			<mescroll-body v-if="currentTab!==0||currentTab==0&&hasAttention==true" ref="mescrollRef"
				@init="mescrollInit" @down="downCallback" @up="upCallback" :up="upOption">
				<view v-if="dataList.length > 0" v-for="(item,index) in dataList" :key="index">
					<view v-if="item.r_type=='post'">
						<post-item @clickTopic="clickTopicAction(item)" @clickForum="jumpToForumPostList(item)"
							:showForum="currentTab==3" v-if="item.show_type==1||item.show_type==4"
							:showImage="item.show_type!==4" @click="jumpDetail(item)" :post="item"
							:isLast="index===dataList.length-1"></post-item>
						<post-item-title @clickForum="jumpToForumPostList(item)" :showForum="currentTab==3"
							v-else-if="item.show_type==3" @click="jumpDetail(item)" :post="item"
							:isLast="index===dataList.length-1"></post-item-title>
						<post-item-image @clickTopic="clickTopicAction(item)" @clickForum="jumpToForumPostList(item)"
							:showForum="currentTab==3" v-else-if="item.show_type==5||item.show_type==6"
							:showSummary="item.show_type==6" @click="jumpDetail(item)" :post="item"
							:isLast="index===dataList.length-1">
						</post-item-image>
					</view>
					<view v-else-if="item.r_type=='recommend_user'">
						<feeds-recommend-user :listIndex="index" @attention="recommendUserAttention" :userList="item.data"></feeds-recommend-user>
					</view>
					<view v-else-if="item.r_type=='recommend_circle'">
						<feeds-recommend-circle :dataList="item.data"></feeds-recommend-circle>
					</view>
					<view v-else-if="item.r_type=='recommend_topic'">
						<feeds-recommend-circle-topic :dataList="item.data"></feeds-recommend-circle-topic>
					</view>
				</view>
			</mescroll-body>
			<publish-choose ref="publishChoose" class="publish-choose"></publish-choose>
			<view class="cu-tabbar-height"></view>
			<!-- 			<uni-fab ref="fab" :pattern="floatMenuBtn.pattern" :content="floatMenuBtn.content" horizontal="right" vertical="bottom" direction="vertical"
			 @trigger="trigger" @fabClick="fabClick"/> -->
			<tui-actionsheet :show="sortMenu.showActionSheet" :tips="sortMenu.tips" :item-list="sortMenu.itemList"
				:mask-closable="sortMenu.maskClosable" :color="sortMenu.color" :size="sortMenu.size"
				:is-cancel="sortMenu.isCancel" @click="itemClick" @cancel="closeActionSheet"></tui-actionsheet>
			<tabbar @publish="publishClicked" :tabCur="1" :unreadMsgCount="unreadMsgCount" :unreadCount="tabUnreadCount"></tabbar>
		</view>
</template>

<script>
	import tabbar from '@/components/tabbar.vue'
	import PostItem from '@/components/post-item.vue';
	import PostItemTitle from '@/components/post-item-title.vue';
	import PostItemImage from '@/components/post-item-image.vue';
	import config from '@/api/config.js'
	import TopicItem from '@/components/topic-item.vue'
	import PublishChoose from '@/components/publish-choose.vue'
	import CircleItem from '@/components/circle-item.vue';
	import RecommendUserItem from '@/components/recommend-user-item.vue'
	import FeedsRecommendUser from '@/components/feeds-recommend-user.vue'
	import FeedsRecommendCircle from '@/components/feeds-recommend-circle.vue'
	import FeedsRecommendCircleTopic from '@/components/feeds-recommend-circle-topic.vue'
	import {
		getTimeDistance,
		chunk,
		formatCount
	} from '@/api/utils.js'
	import {
		postList,
		userUnreadCountInfo,
		getMySubscribeList,
		getTopicList,
		attentionTopic,
		getIndexActivityList,
		getUserAttentionStatus,
		getAdminUnreadCountInfo,
		getPostListBySubscribeForumId,
		getForumList,
		getCircleCategoryList,
		getCircleByCategory,
		attentionUser,
		indexAttentionRecommendList,
		getIndexConfigData,
		getTopNewsList,
		getIndexTotalInfo
	} from '@/api/api.js'
	import {
		getHomeListMode,
		saveHomeListMode,
		getLastHomeRefreshTime,
		saveLastHomeRefreshTime,
		isNeedRefreshNow
	} from '@/api/statusCache.js'
	import {
		isLogin,
		getUserInfo
	} from '@/api/auth.js'
	import {
		checkLogin
	} from '@/js_sdk/checkLogin.js'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	export default {
		components: {
			TopicItem,
			PostItem,
			PostItemTitle,
			PostItemImage,
			tabbar,
			PublishChoose,
			CircleItem,
			RecommendUserItem,
			FeedsRecommendUser,
			FeedsRecommendCircle,
			FeedsRecommendCircleTopic
		},
		mixins: [MescrollMixin],
		name: "Home",
		computed: {
			enableCreateTopic() {
				let userInfo = getUserInfo()
				if (userInfo.role_id > 0) {
					return true
				}
				if (this.appSetting == null || this.appSetting == undefined) {
					return false
				}
				return this.appSetting.enable_user_create_topic == 1
			},
			enableNavForum() {
				if (this.appSetting == null || this.appSetting == undefined) {
					return false
				}
				return this.appSetting.enable_nav_forum == 1
			},
			tabUnreadCount() {
				if (this.adminUnreadInfo == null) {
					return 0
				}
				return this.adminUnreadInfo.total
			},
			fixNavTop() {
				console.log('navHeight:' + this.navHeight)
				return this.CustomBar
			},
			activityTitle() {
				if (this.activityList.length == 0) {
					return ''
				}
				return this.activityList[this.currentActivity].title
			},
			navBgImageHeight() {
				return this.windowWidth * 0.6;
			},
			navHeight() {
				return this.CustomBar
			},
			statusHeight() {
				return this.StatusBar
			},
			navOpacity() {
				return this.scrollTop / (this.CustomBar + 80)
			},
			searchOpacity() {
				return 1 - this.navOpacity
			},
			//===============
			appName() {
				return config.appName
			},
			tabPadding() {
				return 18 * this.ratio
			},
			ratio() {
				return this.windowWidth / 320
			},
			tabWidth() {
				let width = 150 * (this.windowWidth / 320)
				console.log('tabWidth:' + width)
				return width
			},
			listTop() {
				return 35 * (this.windowHeight / 568)
			}
		},
		onShow() {
			this.isUserLogined = isLogin()
			if (isNeedRefreshNow()) {
				if (this.mescroll) {
					this.mescroll.resetUpScroll()
				}
			}
			this.getMyAttentionStatus()
			if (this.isUserLogined) {
				userUnreadCountInfo().then(res => {
					this.unreadMsgCount = res.data.total
				}).catch(error => {

				})
				this.initAdminUnreadInfo()
			}
		},
		onHide(){
			this.$refs.publishChoose.hiddenTabbar()
		},
		onPageScroll(res) {
			this.scrollTop = res.scrollTop
			this.$refs.publishChoose.hiddenTabbar()
		},
		created() {
			uni.hideTabBar()
			this.sortMenu.itemList[this.sortMenu.showType - 1].color = '#0081ff'
		},
		onLoad(options) {
			if (options.tab !== null && options.tab !== undefined && options.tab.length > 0) {
				this.currentTab = parseInt(options.tab)
			}
			this.isUserLogined = isLogin()
			this.initIndexPageConfigData()
			let _this = this
			uni.$on('UserLoginSuccess', function(data) {
				_this.isUserLogined = isLogin()
				_this.getMyAttentionStatus()
			})
			uni.getSystemInfo({
				success(res) {
					_this.windowScale = res.pixelRatio
					_this.windowWidth = res.windowWidth
					console.log('window width:' + res.windowWidth);
					_this.windowHeight = res.windowHeight
					_this.y = (_this.windowHeight / 568) * _this.y
					_this.StatusBar = res.statusBarHeight
					// #ifdef MP-QQ || MP-WEIXIN
					let custom = wx.getMenuButtonBoundingClientRect();
					_this.CustomBar = custom.bottom + custom.top - res.statusBarHeight;
					// #endif
					// #ifdef MP-BAIDU
					let custom = swan.getMenuButtonBoundingClientRect();
					_this.CustomBar = custom.bottom + custom.top - res.statusBarHeight;
					// #endif
				}
			})
		},
		data() {
			return {
				hasShop: false,
				firstInitFloatTop:false,
				totalInfo:null,
				floatMenuFixedTop:305,
				recommendTopicList: [],
				recommendUserList: [],
				dotStyle: {
					bottom: 5,
					backgroundColor: '#aaaaaa',
					selectedBackgroundColor: '#8799a3',
					border: '0px rgba(0, 0, 0, .3) solid',
					selectedBorder: '0px rgba(0, 0, 0, .9) solid',
				},
				forumSwiperCurrent: 0,
				topNews: [],
				floatMenuBtn: {
					pattern: {
						color: '#7A7E83',
						backgroundColor: '#fff',
						selectedColor: '#007AFF',
					},
					content: [{
							iconPath: '/static/images/setting/sign.png',
							selectedIconPath: '/static/images/setting/sign.png',
							text: '签到',
							active: false
						},
						{
							iconPath: '/static/images/setting/water.png',
							selectedIconPath: '/static/images/setting/water.png',
							text: '灌水',
							active: false
						}
					]
				},
				forumList: [],
				subscribeTab: {
					scrollLeft: 0,
					items: [],
					current: 0,
				},
				circleTab: {
					scrollLeft: 0,
					items: [],
					current: 0,
				},
				appSetting: this.AppSetting,
				subscribeForumList: [],
				adminUnreadInfo: null,
				unreadMsgCount: 0,
				hasAttention: false,
				//====
				currentActivity: 0,
				activityList: [],
				top: 0, //标题图标距离顶部距离
				opacity: 0.5,
				scrollTop: 0.5,
				//========
				mySubscribeCount: 0,
				themeColor: this.ThemeColor,
				isUserLogined: false,
				hasSubscribe: false,
				sortMenu: {
					lastShowType: null,
					showType: 2,
					showActionSheet: false,
					maskClosable: true,
					itemList: [{
							text: "标准模式",
							color: "#2B2B2B"
						},
						{
							text: "自动调节",
							color: "#2B2B2B"
						}, {
							text: "纯标题",
							color: "#2B2B2B"
						}, {
							text: "标题概要",
							color: "#2B2B2B"
						}, {
							text: "图享模式",
							color: "#2B2B2B"
						}, {
							text: "图文模式",
							color: "#2B2B2B"
						}
					],
					color: "#9a9a9a",
					size: 26,
					isCancel: true
				},
				lastUpdateTime: null,
				windowScale: 0,
				windowWidth: 0,
				windowHeight: 0,
				StatusBar: this.StatusBar,
				CustomBar: this.CustomBar,
				dataList: [],
				currentTab: 1,
				tabs: [
					"关注",
					"最新",
					"热门",
				],
				upOption: {
					page: {
						size: 10 // 每页数据的数量,默认10
					},
					textNoMore: config.customNoMore,
					noMoreSize: 1, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						tip: '暂无相关数据'
					}
				},
			}
		},
		methods: {
			jumpToMall() 
			{
				uni.navigateTo({
					url:'/pages/always/create-order/create-order?shopId=1'
				})
			},
			format(count) 
			{
				return formatCount(count)
			},
			initIndexPageConfigData() 
			{
				getIndexConfigData().then(res=>{
					this.activityList = res.data.activity_list 
					this.topNews = res.data.post_list
					this.totalInfo = res.data.total
					this.forumList = []
					this.hasShop = res.data.has_shop
					let chunkList = chunk(res.data.forum_list, 6)
					chunkList.forEach(list => {
						let subChunkList = chunk(list, 3)
						this.forumList.push(subChunkList)
					})
					let that = this
					this.$nextTick(function(){
						that.getFloatMenuTop()
					})
				}).catch(error=>{
					
				})
			},
			recommendUserAttention(passParams) 
			{
				if (isLogin() == false) {
					let that = this
					uni.navigateTo({
						url: '/pages/login/login'
					})
					return
				}
				let listIndex = passParams[0]
				let subIndex = passParams[1]
				let item = this.dataList[listIndex].data[subIndex]
				let status = item.is_attention==1? 0:1
				uni.showLoading({
					title: '正在提交...'
				})
				attentionUser(item.user_id,status).then(res => {
					uni.hideLoading()
					item.is_attention = status
					let tip = status == 1 ? '已关注' : '已取消'
					uni.showToast({
						title: tip
					})
				}).catch(error => {
					uni.hideLoading()
				})
			},
			initAttentionRecommendList() 
			{
				indexAttentionRecommendList().then(res=>{
					this.recommendUserList = chunk(res.data.user_list,3) 
					this.recommendTopicList = res.data.topic_list
				}).catch(error=>{
					
				})
			},
			getFloatMenuTop() 
			{
				if(this.firstInitFloatTop == false) {
					let that = this
					let query = uni.createSelectorQuery().in(this)
					query.select('.float-menu').boundingClientRect(data=>{
						console.log("得到布局位置信息" + JSON.stringify(data));
						  console.log("节点离页面顶部的距离为" + data.top);
						  that.floatMenuFixedTop = data.top - that.CustomBar
					}).exec()
					this.firstInitFloatTop = true
				}
			},
			jumpToAllSort() 
			{
				uni.navigateTo({
					url:'/pages/lessuse/all-sort/all-sort'
				})
			},
			jumpToSignSort() 
			{
				uni.navigateTo({
					url:'/pages/lessuse/sign-sort/sign-sort'
				})
			},
			jumpTopicList() 
			{
				uni.navigateTo({
					url:'/pages/always/topic-list/topic-list?mode=show'
				})
			},
			confirmAttentionAction() 
			{
				this.getMyAttentionStatus()
			},
			attentionUserAction(item,index,subIndex) {
				if (isLogin() == false) {
					let that = this
					uni.navigateTo({
						url: '/pages/login/login'
					})
					return
				}
				uni.showLoading({
					title: '正在提交...'
				})
				let status = item.is_attention==1? 0:1
				attentionUser(item.user_id,status).then(res => {
					uni.hideLoading()
					this.recommendUserList[index][subIndex].is_attention = status
					let tip = status == 1 ? '已关注' : '已取消'
					uni.showToast({
						title: tip
					})
				}).catch(error => {
					uni.hideLoading()
				})
			},
			changeForumSwiper(e) {
				this.forumSwiperCurrent = e.detail.current
			},
			jumpToCircleProfile(item) {
				uni.navigateTo({
					url: '/pages/lessuse/circle-profile/circle-profile?circleId=' + item.circle_id
				})
			},
			publishClicked() {
				this.$refs.publishChoose.switchStatus()
			},
			trigger(e) {
				console.log(e)
				this.content[e.index].active = !e.item.active
				uni.showModal({
					title: '提示',
					content: `您${this.content[e.index].active ? '选中了' : '取消了'}${e.item.text}`,
					success: function(res) {
						if (res.confirm) {
							console.log('用户点击确定')
						} else if (res.cancel) {
							console.log('用户点击取消')
						}
					}
				})
			},
			fabClick() {
				uni.showToast({
					title: '点击了悬浮按钮',
					icon: 'none'
				})
			},
			initAllForumList() {
				getForumList().then(res => {
					this.subscribeTab.items = this.subscribeTab.items.concat(res.data)
					console.log('get forum list:')
					console.log(res.data)
					let chunkList = chunk(res.data, 6)
					chunkList.forEach(list => {
						let subChunkList = chunk(list, 3)
						this.forumList.push(subChunkList)
					})
				}).catch(error => {

				})
			},
			tabSelect(e) {
				if (this.currentTab == 3) {
					this.subscribeTab.current = e.currentTarget.dataset.id;
					this.subscribeTab.scrollLeft = (e.currentTarget.dataset.id - 1) * 60
					if (this.mescroll) {
						this.mescroll.triggerDownScroll()
					}
				}
				if (this.currentTab == 2) {
					this.circleTab.current = e.currentTarget.dataset.id;
					this.circleTab.scrollLeft = (e.currentTarget.dataset.id - 1) * 40
					if (this.mescroll) {
						this.mescroll.triggerDownScroll()
					}
				}
			},
			jumpToCreateTopic() {
				if (isLogin() == false) {
					checkLogin()
					return
				}
				uni.navigateTo({
					url: '/pages/always/create-topic/create-topic'
				})
			},
			jumpToForumPostListAtIndex(index) {
				let trueIndex = index - 5;
				let forum = this.subscribeForumList[trueIndex]
				uni.navigateTo({
					url: '/pages/lessuse/forum-post-list/forum-post-list?forumId=' + forum.forum_id +
						'&forumName=' + forum.name
				})
			},
			initAdminUnreadInfo() {
				let userInfo = getUserInfo()
				if (userInfo.role && userInfo.role_id > 0) {
					getAdminUnreadCountInfo().then(res => {
						this.adminUnreadInfo = res.data
					}).catch(error => {

					})
				}
			},
			getMyAttentionStatus() {
				if (isLogin()) {
					getUserAttentionStatus().then(res => {
						this.hasAttention = res.data.status == 1
					}).catch(error => {

					})
				}
			},
			jumpToSearchAction() {
				uni.navigateTo({
					url: '/pages/lessuse/search/search'
				})
			},
			jumpToActivity(activity) {
				if (activity.jump_path && activity.jump_path.length > 0) {
					uni.navigateTo({
						url: activity.jump_path
					})
					return
				}
			},
			changeActivity(e) {
				this.currentActivity = e.detail.current
			},
			getActivityList() {
				getIndexActivityList().then(res => {
					this.activityList = res.data
				}).catch(error => {

				})
			},
			loginNowAction() {
				checkLogin()
			},
			jumpToForumList() {
				uni.navigateTo({
					url: '/pages/always/forum-list/forum-list'
				})
			},
			chooseMoreItem(index) {
				if (index == 1) {
					this.sortMenu.showActionSheet = true
					return
				}
				if (index == 0) {
					uni.navigateTo({
						url: '/pages/lessuse/search/search'
					})
				}
			},
			jumpToUserProfile(userId) {
				uni.navigateTo({
					url: '/pages/profile/profile?userId=' + userId
				})
			},
			attentionTopicAction(item, index) {
				if (isLogin() == false) {
					checkLogin()
					return
				}
				uni.showLoading({
					title: '正在提交...'
				})
				let status = item.is_attention == 0 ? 1 : 0
				attentionTopic(item.topic_id, status).then(res => {
					uni.hideLoading()
					this.recommendTopicList[index].is_attention = status
					let tip = status == 1 ? '已关注' : '已取消'
					uni.showToast({
						title: tip
					})
				}).catch(error => {
					uni.hideLoading()
				})
			},
			clickTopicItem(item) {
				console.log('topic is ' + item.title);
				uni.navigateTo({
					url: '/pages/always/topic-post-list/topic-post-list?topicId=' + item.topic_id,
					success: function(res) {
						res.eventChannel.emit('topicPostList', item)
					}
				})
			},
			clickTopicAction(item) {
				console.log('topic is ' + item.topic.title);
				uni.navigateTo({
					url: '/pages/always/topic-post-list/topic-post-list?topicId=' + item.topic.topic_id,
					success: function(res) {
						res.eventChannel.emit('topicPostList', item.topic)
					}
				})
			},
			jumpToForumPostListByItem(item) {
				uni.navigateTo({
					url: '/pages/lessuse/forum-post-list/forum-post-list?forumId=' + item.forum_id +
						'&forumName=' + item.name
				})
			},
			jumpToForumPostList(item) {
				uni.navigateTo({
					url: '/pages/lessuse/forum-post-list/forum-post-list?forumId=' + item.forum.forum_id +
						'&forumName=' + item.forum.name
				})
			},
			getMySubscribeStatus() {
				let isLogined = isLogin()
				if (isLogined) {
					getMySubscribeList().then(res => {
						this.mySubscribeCount = res.data.length
						this.subscribeForumList = res.data
						if (res.data.length > 0) {
							this.hasSubscribe = true
							if (this.enableNavForum) {
								this.tabs = [
									"热榜",
									"最新",
									"话题",
									"订阅",
									"关注"
								]
								res.data.forEach(forum => {
									this.tabs.push(forum.name)
								})
								if (this.currentTab > 4) {
									this.currentTab = 0 //在别的页面发生了订阅或者非订阅行为
									if (this.mescroll) {
										this.mescroll.resetUpScroll()
									}
								}
							}
						} else {
							if (this.currentTab == 3) {
								this.dataList = []
							}
							this.hasSubscribe = false
						}
					}).catch(error => {})
				} else {}
			},
			onShareAppMessage(res) {
				return {
					title: config.indexShareTitle,
					path: '/pages/index/index'
				}
			},
			onShareTimeline(res) {
				return {
					title: config.indexShareTitle,
					path: '/pages/index/index'
				}
			},
			getRandomNum(Min, Max) {
				var Range = Max - Min;
				var Rand = Math.random();
				let rand = (Min + Math.round(Rand * Range));
				return rand
			},
			adjustShowType(post) {
				if (post.sort_index > 0) {
					return 3
				}
				if (post.image_list == null || post.image_list.length == 0) {
					if (post.summary.length < 15) {
						if (post.topic_id > 0) {
							return 1
						}
						return 3
					}
					return 4
				}
				if (post.link && post.link.indexOf('.mp4') !== -1) {
					return 1
				}
				if (post.image_list.length == 1) {
					return 1
				}
				//图片
				if (post.summary == null || post.summary.length < 15) {
					if (post.topic_id > 0) {
						return 1
					}
					let canTypes = [5, 6]
					let randomType = this.getRandomNum(0, 1);
					return canTypes[randomType]
				}
				if (post.image_list.length == 2) {
					let canTypes = [1, 5]
					if (post.comment_count < 10) {
						let randomType = this.getRandomNum(0, 1);
						return canTypes[randomType]
					}
					canTypes = [5, 6]
					let randomType = this.getRandomNum(0, 1);
					return canTypes[randomType]
				}
				if (post.image_list.length > 2) {
					let canTypes = [5, 6]
					let randomType = this.getRandomNum(0, 1);
					return canTypes[randomType]
				}
				return 1
			},
			closeActionSheet: function() {
				this.sortMenu.showActionSheet = false
			},
			itemClick: function(e) {
				this.sortMenu.lastShowType = this.sortMenu.showType
				let showType = e.index + 1;
				this.sortMenu.showType = showType;
				this.sortMenu.itemList[e.index].color = '#0081ff'
				this.sortMenu.itemList[this.sortMenu.lastShowType - 1].color = '#2B2B2B'
				saveHomeListMode(showType)
				this.closeActionSheet();
			},
			searchAction() {
				uni.navigateTo({
					url: '/pages/search/search'
				})
			},
			jumpDetail(post) {
				console.log(post)
				uni.navigateTo({
					url: '/pages/detail/detail?postId=' + post.post_id
				})
			},
			initFeedsList() {
				this.getDataList(0, 10);
			},
			changeTab(index) {
				this.currentTab = index
				if(this.currentTab == 0) {
					if(!this.hasAttention&&isLogin()) {
						this.initAttentionRecommendList()
					}
					return
				}
				if (this.currentTab == 2) {
					this.dataList = []
				}
				if (this.mescroll) {
					this.mescroll.triggerDownScroll()
				}
			},
			getDataList(pageIndex, pageSize) {
				// if (this.currentTab == 2) {
				// 	let categoryId = this.circleTab.items[this.circleTab.current].category_id
				// 	getCircleByCategory(categoryId,pageIndex,pageSize).then(res => {
				// 		let pageList = res.data.list
				// 		if (pageIndex == 0) {
				// 			this.dataList = pageList
				// 		} else {
				// 			this.dataList = this.dataList.concat(pageList);
				// 		}
				// 		console.log('page length:' + pageList.length)
				// 		let hasNext = pageList.length >= pageSize
				// 		if (this.mescroll) {
				// 			this.mescroll.endSuccess(pageList.length, hasNext);
				// 		}
				// 	}).catch(error => {
				// 		if (this.mescroll) {
				// 			this.mescroll.endErr()
				// 		}
				// 	})
				// } else {
				if (this.currentTab == 0 || this.currentTab == 1 || this.currentTab == 2) {
					let type = 5
					if (this.currentTab == 1) {
						type = 2
					}
					if (this.currentTab == 2) {
						type = 1
					}
					postList(type, pageIndex, pageSize).then(res => {
						let index = 0
						res.data.list.forEach(item => {
							//最多显示三张图片
							if (item.image_list && item.image_list.length > 3) {
								item.image_list = item.image_list.slice(0, 3)
							}
							item.show_type = this.adjustShowType(item)
							index++
							if (item.image_list && item.image_list.length > 0) {
								item.icon = 'pic'
							}
							item.show_time = getTimeDistance(item.created_at)
							item.r_type = 'post'
						})
						let pageList = res.data.list
						//有没有推荐数据，有的话处理推荐数据
						if(res.data.recommend) {
							let recommend = res.data.recommend
							let hasUser = recommend.user_list.length>0
							let hasCircle = recommend.circle_list.length>0
							let hasTopic = recommend.topic_list.length>0 
							let item = {}
							if(hasUser) {
								item = {
									'r_type':'recommend_user',
									'data':recommend.user_list
								}
							}else if(hasCircle) {
								item = {
									'r_type':'recommend_circle',
									'data':recommend.circle_list
								}
							}else if(hasTopic) {
								item = {
									'r_type':'recommend_topic',
									'data':recommend.topic_list
								}
							}
							if(pageList.length<=1) {
								pageList.push(item)
							}else{
								pageList.splice(pageList.length-2,1,item)
							}
						}
						if (pageIndex == 0) {
							this.dataList = pageList
							saveLastHomeRefreshTime()
						} else {
							this.dataList = this.dataList.concat(pageList);
						}
						console.log('page length:' + pageList.length)
						let hasNext = pageList.length >= pageSize
						if (this.mescroll) {
							this.mescroll.endSuccess(pageList.length, hasNext);
						}
					}).catch(error => {
						if (this.mescroll) {
							this.mescroll.endErr()
						}
					})
				} else if (this.currentTab == 3) {
					if (this.subscribeTab.current > 0) {
						let forumId = this.subscribeTab.items[this.subscribeTab.current].forum_id
						getPostListBySubscribeForumId(forumId, pageIndex, pageSize, 1).then(res => {
							res.data.list.forEach(item => {
								//最多显示三张图片
								if (item.image_list && item.image_list.length > 3) {
									item.image_list = item.image_list.slice(0, 3)
								}
								item.show_type = this.adjustShowType(item)
								if (item.image_list && item.image_list.length > 0) {
									item.icon = 'pic'
								}
								item.show_time = getTimeDistance(item.created_at)
							})
							let pageList = res.data.list
							if (pageIndex == 0) {
								this.dataList = pageList
								saveLastHomeRefreshTime()
							} else {
								this.dataList = this.dataList.concat(pageList);
							}
							console.log('page length:' + pageList.length)
							let hasNext = pageList.length >= pageSize
							if (this.mescroll) {
								this.mescroll.endSuccess(pageList.length, hasNext);
							}
						}).catch(error => {
							if (this.mescroll) {
								this.mescroll.endErr()
							}
						})
					} else {

					}
				}
				// }
			},
			downCallback() {
				if (isLogin() == true) {
					userUnreadCountInfo().then(res => {
						this.unreadMsgCount = res.data.total
					}).catch(error => {

					})
					this.initAdminUnreadInfo()
				}
				getIndexTotalInfo().then(res=>{
					this.totalInfo = res.data.total
					this.topNews = res.data.post_list
				}).catch(error=>{
					
				})
				if (this.mescroll) {
					this.mescroll.resetUpScroll()
				}
			},
			upCallback(page) {
				let pageIndex = page.num - 1
				this.getDataList(pageIndex, page.size)
			},
		}
	}
</script>

<style scoped>
	.header {
		padding: 80rpx 90rpx 60rpx 90rpx;
		box-sizing: border-box;
	}

	.title {
		font-size: 34rpx;
		color: #333;
		font-weight: 500;
	}

	.sub-title {
		font-size: 24rpx;
		color: #7a7a7a;
		padding-top: 18rpx;
	}

	.tui-primary {
		color: #5677FC;
	}

	.header-bg {
		width: 100%;
		height: 100%;
		position: relative;
		z-index: 2;
		background-size: cover;
		background-repeat: no-repeat;
	}

	.swiper {
		position: absolute;
		z-index: 3;
	}

	.banner-image {
		width: 100%;
		height: 100%;
		position: relative;
		background-size: cover;
		background-repeat: no-repeat;
	}

	.publish-choose {
		position: fixed;
		width: 100%;
		bottom: 130upx;
		z-index: 111;
	}

	.middle-icon {
		width: 32px;
		height: 32px;
	}
	
	.float-menu {
	}
	
</style>
